const Router = require("koa-router");
const multer = require("koa-multer");
const path = require("path");
let router = new Router();
let storage = multer.diskStorage({
  //文件保存路径
  destination: function (req, file, cb) {
    cb(null, path.join(__dirname, "../../public/uploads/")); //注意路径必须存在
  },
  //修改文件名称
  filename: function (req, file, cb) {
    let fileFormat = file.originalname.split(".");
    cb(null, Date.now() + "." + fileFormat[fileFormat.length - 1]);
  },
});
let upload = multer({ storage: storage });

router.post("/upload", upload.single("file"), async (ctx) => {
  ctx.body = {
    filename: ctx.req.file.filename, //返回文件名
    body: ctx.req.body,
  };
});

module.exports = router;
